package com.friends.common.tools.util;


import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.util.*;

/**
 * Created by xiajiangge on 2017/2/15.
 */
public class Tools {

    public static String getUUID(){
        String id= UUID.randomUUID().toString();
        id=id.replace("-", "");
        return id;
    }

    public static String MD5(String inStr) {
        try{
            MessageDigest md5 = MessageDigest.getInstance("MD5");

            byte[] byteArray = inStr.getBytes("UTF-8");
            byte[] md5Bytes = md5.digest(byteArray);
            StringBuffer hexValue = new StringBuffer();
            for (int i = 0; i < md5Bytes.length; i++) {
                int val = ((int) md5Bytes[i]) & 0xff;
                if (val < 16) {
                    hexValue.append("0");
                }
                hexValue.append(Integer.toHexString(val));
            }
            return hexValue.toString();
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }


    /**
     * 通过按位与运算，计算需要打印的选项
     * 0或1 打印类的属性
     * 0或2 打印类中声明的类
     * 0或4 打印类的方法
     * 例如3表示 打印类的属性和类中声明的类
     * @param o    需要打印的对象
     * @param sum  需要打印的和值
     */
    public static void helpFunc(Object o, int sum) {
        System.out.println("_____________ start dump __________________");
        String className = o.getClass().getName();
        if ((sum & 1) == 1) {
            Field[] fields = o.getClass().getDeclaredFields();
            System.out.println("类" + className + "中声明的field属性");
            for (int i = 0; i < fields.length; ++i) {
                fields[i].setAccessible(true);
                try {
                    System.out.println(fields[i].getName() + "-" + fields[i].toString() + "-" + fields[i].get(o));
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }

        if ((sum & 2) == 2) {
            Class[] classes = o.getClass().getDeclaredClasses();
            System.out.println("类" + className + "中声明的类");
            for (int i = 0; i < classes.length; ++i) {
                try {
                    System.out.println(classes[i].getName() + "-" + classes[i].toString());
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }

        if ((sum & 4) == 4) {
            Method[] methods = o.getClass().getDeclaredMethods();
            System.out.println("类" + className + "中声明的方法");
            for (int i = 0; i < methods.length; ++i) {
                try {
                    System.out.println(methods[i].getName() + "-" + methods[i].toString());
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }
        System.out.println("_____________ end dump __________________");
    }

    public static Map<String, Object> transBean2Map(Object obj) {

        if(obj == null){
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                // 过滤class属性
                if (!key.equals("class")) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);

                    map.put(key, value);
                }

            }
        } catch (Exception e) {
            System.out.println("transBean2Map Error " + e);
        }

        return map;

    }

}
